package com.augurit.gzcgwsp.hwgz.service.impl;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.augurit.common.system.web.auth.form.LoginUserForm;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.lang.StringUtils;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.orm.Page;
import com.augurit.gzcgwsp.excelimportexport.util.ExcelUtil;
import com.augurit.gzcgwsp.excelimportexport.util.FileUtil;
import com.augurit.gzcgwsp.excelimportexport.util.GetFileName;
import com.augurit.gzcgwsp.hwgz.convert.YwHwShSqbConvertor;
import com.augurit.gzcgwsp.hwgz.dao.YwHwShSqbDao;
import com.augurit.gzcgwsp.hwgz.entity.YwHwShSqb;
import com.augurit.gzcgwsp.hwgz.service.IYwHwShSqbService;
import com.augurit.gzcgwsp.hwgz.web.form.YwHwShSqbForm;
import com.augurit.gzcgwsp.sendNotice.service.IExchangeStateService;
import com.augurit.gzcgwsp.sendNotice.service.IYwMtbjlshInfoService;
import com.augurit.gzcgwsp.sendNotice.service.IYwWebserviceForWbService;
import com.augurit.gzcgwsp.sendNotice.service.IYwWebserviceInfoService;
import com.augurit.gzcgwsp.wfex.common.service.IWfServiceEX;
import com.augurit.gzcgwsp.ywcmn.service.IYwComQyxxService;
import com.augurit.gzcgwsp.ywcmn.service.IYwComSjclService;
import com.augurit.gzcgwsp.ywcmn.service.IYwComSjcllistService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComSjclForm;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComSjcllistForm;
import com.augurit.wf.bi.web.form.WfBusInstanceTaskForm;
import com.augurit.wf.common.service.impl.WfBusClientServiceImpl;

@Service
@Transactional
public class YwHwShSqbServiceImpl extends WfBusClientServiceImpl<YwHwShSqbForm> implements IYwHwShSqbService{
    private static final Log log=LogFactory.getLog(YwHwShSqbServiceImpl.class);
	@Autowired
	private YwHwShSqbDao ywHwShSqbDao;
    @Autowired
    private IYwComQyxxService ywComQyxxService;
    @Autowired
    private IYwComSjclService sjclService;
    @Autowired
	private IYwComSjcllistService ywComSjcllistService;
	@Autowired
	private IYwComSjclService ywComSjclService;
	@Autowired
    private IYwMtbjlshInfoService ywMtbjlshInfoService;
    @Autowired
	private IYwWebserviceInfoService ywWebserviceInfoService;
    @Autowired
    private IYwWebserviceForWbService ywWebserviceForWbService;
    @Autowired
    private IExchangeStateService exchangeStateService;
    @Autowired
    private IWfServiceEX wfServiceEX;

	
	public String writeSslybData(String context,String printtype,YwHwShSqbForm xkForm) {
	    String inpath=context+"/Templates/hwgg/template/gc.xls";
	    if("wq".equals(printtype)){
	    	inpath=context+"/Templates/hwgg/template/hwggszz.xls";
	    }else if("dht".equals(printtype)){
	    	inpath=context+"/Templates/hwgg/template/dht.xls";
	    }
	    String nodeStr = System.getProperty("file.separator"); 
	    inpath=inpath.replace("/",nodeStr);
	    String tempOutPath="Templates/hwgg/outfile";
	    String path="";
	    try {
	        path = this.writeExcelData(inpath, tempOutPath, context,printtype,xkForm);
	        String paths=context+"/"+path;
	        paths=paths.replace("/", File.separator);
	        String dir=paths.substring(0,paths.lastIndexOf(File.separator));
	        String filename=paths.substring(paths.lastIndexOf(File.separator)+1,paths.length());
	        try {//删除其他文件
	            FileUtil.delDir(dir,filename);
	        } catch (IOException e) {
	            log.error("删除文件错误"+e);
	        }
	    } catch (Exception e) {
	      log.error("写入数据错误"+e);
	    }
	    return  path;
	}
	
	
	private String writeExcelData(String inpath, String realpath,
	        String context,String printtype,YwHwShSqbForm xkForm) {
	        String fileName = GetFileName.getFileName("xls");
	        String dateDir = GetFileName.getDirector();
	        String returnpath=realpath+"/"+dateDir+"/"+fileName;
	        String path=context+"/"+returnpath;
	        path=path.replace("/",File.separator);
	        String outdir=path.substring(0,path.lastIndexOf(File.separator));
	       
	        File file=new File(outdir);
	        if(!file.exists()){
	            file.mkdirs();
	        }
	        writerSsbsDataToExcel(inpath,path,printtype,xkForm);
	       return returnpath;
	}
	
	
	private void writerSsbsDataToExcel(String inpath, String path,String printtype,YwHwShSqbForm xkForm) {
	    WritableWorkbook wwb;
	    try {
	        wwb = ExcelUtil.createWorkbookByOld(inpath, path);
	        WritableSheet wsOne = ExcelUtil.getWritableSheetByIndex(wwb, 0);
	        copyssbsData(wsOne, printtype,xkForm);
	        wsOne.setProtected(true);
	        wwb.write();
	        wwb.close();
	    } catch (Exception e) {
	        log.error("写文件错误"+e);
	    } 
	    
	}
	
	
	

	private void copyssbsData(WritableSheet wsOne,String printtype, YwHwShSqbForm form) {
	     try {
	            WritableFont font = new WritableFont(WritableFont.createFont("宋体"),10);
	            WritableCellFormat wcf = new WritableCellFormat(font);
	            wcf.setAlignment(Alignment.LEFT);
	            wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
	            WritableFont font2 = new WritableFont(WritableFont.createFont("宋体"),18);
	            WritableCellFormat wcf2 = new WritableCellFormat(font2);
	            wcf2.setAlignment(Alignment.RIGHT);
	            wcf2.setVerticalAlignment(VerticalAlignment.CENTRE);
	            
	            WritableFont font1 = new WritableFont(WritableFont.createFont("宋体"),22);
	            WritableCellFormat wcf1 = new WritableCellFormat(font1);
	            wcf1.setAlignment(Alignment.LEFT);
	            wcf1.setVerticalAlignment(VerticalAlignment.CENTRE);   
	        
	            WritableFont font4 = new WritableFont(WritableFont.createFont("宋体"),16);
	            WritableCellFormat wcf4 = new WritableCellFormat(font4);
	            wcf4.setAlignment(Alignment.LEFT);
	            wcf4.setVerticalAlignment(VerticalAlignment.CENTRE);
	            
	            
	            Calendar cal=Calendar.getInstance();
	            int year=cal.get(Calendar.YEAR);
	            int month=cal.get(Calendar.MONTH)+1;
	            int day=cal.get(Calendar.DATE);
	            String zsbh="穗城管委户外广设字["+year+"]"+form.getSzzh()+"号";
	            String address="见附表4:["+form.getSqdw()+"户外广设字(["+year+"]"+form.getSzzh()+")]";
	            if("wq".equals(printtype)){
	            	  zsbh="穗城管委围墙户外广设字["+year+"]"+form.getSzzh()+"号";
	            	  address="见附表4:["+form.getSqdw()+"工地围墙广告设置地址(对应设置证编号为["+year+"]"+form.getSzzh()+")]";
	   	           
	            }else if("dht".equals(printtype)){
	            	address="见附表5:["+form.getSqdw()+"电话亭广告设置地址(对应设置证编号为["+year+"]"+form.getSzzh()+")]";
	            }
	        if("dht".equals(printtype)){
	            	ExcelUtil.modiStrCell2(wsOne, 3, 3, zsbh, wcf1);//编号
	  	            ExcelUtil.modiStrCell2(wsOne, 2, 5, form.getSqdw(), wcf1);//设置单位
	  	            ExcelUtil.modiStrCell2(wsOne, 5, 7, form.getFrxm(), wcf1);//法定代表人
	  	            ExcelUtil.modiStrCell2(wsOne, 1, 9, address, wcf);//地址
	  	            ExcelUtil.modiStrCell2(wsOne, 2, 11, form.getZbdw(), wcf1);//中标单位
	  	            ExcelUtil.modiStrCell2(wsOne, 2, 13, form.getGgxsStr(), wcf1);//设置类型
	  	            ExcelUtil.modiStrCell2(wsOne, 2, 15, form.getSzggStr(), wcf4);//设置规格
	  	            ExcelUtil.modiStrCell2(wsOne, 3, 17, form.getStartyear(), wcf2);//开始年
	  	            ExcelUtil.modiStrCell2(wsOne, 5, 17, form.getStartmonth(), wcf2);//
	  	            ExcelUtil.modiStrCell2(wsOne, 7, 17, form.getStartday(), wcf2);//设置期限
	  	            ExcelUtil.modiStrCell2(wsOne, 10, 17, form.getEndyear(), wcf2);//开始年
	  	            ExcelUtil.modiStrCell2(wsOne, 12, 17, form.getEndmonth(), wcf2);//
	  	            ExcelUtil.modiStrCell2(wsOne, 14, 17, form.getEndday(), wcf2);//设置期限
	  	            ExcelUtil.modiStrCell2(wsOne, 12, 23, form.getPrintyear(), wcf2);//打印时间
	  	            ExcelUtil.modiStrCell2(wsOne, 14, 23, form.getPrintmonth(), wcf2);//打印时间
	  	            ExcelUtil.modiStrCell2(wsOne, 16, 23, form.getPrintday(), wcf2);//打印时间
	            }else if("wq".equals(printtype)){//写围墙
	            	 ExcelUtil.modiStrCell2(wsOne, 5, 3, zsbh, wcf1);//编号
			            ExcelUtil.modiStrCell2(wsOne, 4, 5, form.getSqdw(), wcf1);//设置人名称
			            ExcelUtil.modiStrCell2(wsOne, 2, 7, address, wcf);//地址
			            ExcelUtil.modiStrCell2(wsOne, 5, 9, form.getFrxm(), wcf1);//法定代表人
			            ExcelUtil.modiStrCell2(wsOne, 2, 11, form.getGgxsStr(), wcf1);//设置类型
			            ExcelUtil.modiStrCell2(wsOne, 2, 13, form.getSzggStr(), wcf4);//设置规格
			            ExcelUtil.modiStrCell2(wsOne, 4, 15, form.getStartyear(), wcf2);//开始年
			            ExcelUtil.modiStrCell2(wsOne, 6, 15, form.getStartmonth(), wcf2);//
			            ExcelUtil.modiStrCell2(wsOne, 8, 15, form.getStartday(), wcf2);//设置期限
			            ExcelUtil.modiStrCell2(wsOne, 11, 15, form.getEndyear(), wcf2);//开始年
			            ExcelUtil.modiStrCell2(wsOne, 13, 15, form.getEndmonth(), wcf2);//
			            ExcelUtil.modiStrCell2(wsOne, 15, 15, form.getEndday(), wcf2);//设置期限
			            ExcelUtil.modiStrCell2(wsOne, 12, 21, form.getPrintyear(), wcf2);//打印时间
			            ExcelUtil.modiStrCell2(wsOne, 15, 21, form.getPrintmonth(), wcf2);//打印时间
			            ExcelUtil.modiStrCell2(wsOne, 17, 21, form.getPrintday(), wcf2);//打印时间
	            }else{
	            	 ExcelUtil.modiStrCell2(wsOne, 3, 3, zsbh, wcf1);//编号
		            ExcelUtil.modiStrCell2(wsOne, 2, 5, form.getSqdw(), wcf1);//设置人名称
		            ExcelUtil.modiStrCell2(wsOne, 1, 7, address, wcf);//地址
		            ExcelUtil.modiStrCell2(wsOne, 5, 9, form.getFrxm(), wcf1);//法定代表人
		            ExcelUtil.modiStrCell2(wsOne, 2, 11, form.getGgxsStr(), wcf1);//设置类型
		            ExcelUtil.modiStrCell2(wsOne, 2, 13, form.getSzggStr(), wcf4);//设置规格
		            ExcelUtil.modiStrCell2(wsOne, 3, 15, form.getStartyear(), wcf2);//开始年
		            ExcelUtil.modiStrCell2(wsOne, 5, 15, form.getStartmonth(), wcf2);//
		            ExcelUtil.modiStrCell2(wsOne, 7, 15, form.getStartday(), wcf2);//设置期限
		            ExcelUtil.modiStrCell2(wsOne, 10, 15, form.getEndyear(), wcf2);//开始年
		            ExcelUtil.modiStrCell2(wsOne, 12, 15, form.getEndmonth(), wcf2);//
		            ExcelUtil.modiStrCell2(wsOne, 14, 15, form.getEndday(), wcf2);//设置期限
		            ExcelUtil.modiStrCell2(wsOne, 10, 21, form.getPrintyear(), wcf2);//打印时间
		            ExcelUtil.modiStrCell2(wsOne, 12, 21, form.getPrintmonth(), wcf2);//打印时间
		            ExcelUtil.modiStrCell2(wsOne, 14, 21, form.getPrintday(), wcf2);//打印时间
		  
	            }
	        } catch (Exception e) {
	            log.error("写入设施一览表的数据错误"+e);
	        }        
	    
	}
	/**
	 * ��������ȡForm����
	 */
	@Transactional(readOnly = true)
	public YwHwShSqbForm get(Long id) {
		YwHwShSqb entity = ywHwShSqbDao.get(id);
		YwHwShSqbForm form = YwHwShSqbConvertor.convertVoToForm(entity);
        YwComQyxxForm qyxxform = ywComQyxxService.get(form.getQyid());
        YwHwShSqbConvertor.convertQyxxFormToForm(qyxxform, form);
        
		YwComSjcllistForm sjclListForm=new YwComSjcllistForm();
		sjclListForm.setAjid(id);
		sjclListForm.setTplcode("hwggshsqb");
        List<YwComSjcllistForm> sjclList = ywComSjcllistService.getList(sjclListForm);
        YwComSjclForm fkForm = new YwComSjclForm();
        fkForm.setFkid(form.getQyid());
        fkForm.setAjid(id);
        fkForm.setTplcode("hwggshsqb");
        List<YwComSjclForm> sjclTpl = ywComSjclService.getList(fkForm);
        for (YwComSjcllistForm listForm : sjclList) {
            boolean hasExists = false;
            for (YwComSjclForm tplForm : sjclTpl) {
                if (tplForm.getTplid() != null && listForm.getId().longValue() == tplForm.getTplid().longValue()) {
                    if (StringUtils.isNotBlank(tplForm.getFilepath())) {
                        listForm.setState("1");
                        listForm.setHasfile("1");
                        listForm.setFilename(tplForm.getFilename());
                        listForm.setFilepath(tplForm.getFilepath());
                        listForm.setFiletype(tplForm.getFiletype());
                    } else {
                        listForm.setState("0");
                        listForm.setHasfile("0");
                    }
                    if (tplForm.getYjs() != null) {
                        listForm.setYjs(Long.valueOf(tplForm.getYjs()));
                    }
                    if (tplForm.getFyjs() != null) {
                        listForm.setFyjs(Long.valueOf(tplForm.getFyjs()));
                    }
                    listForm.setClid(tplForm.getId());
                    if ("1".equals(listForm.getSfdz())) {
                        listForm.setDisabled("1");
                    } else {
                        listForm.setDisabled("0");
                        if (tplForm.getSfdz() == null) {
                            listForm.setSfdz("0");
                        } else {
                            listForm.setSfdz(tplForm.getSfdz());
                        }
                    }
                    hasExists = true;
                }
            }
            if (!hasExists) {
                listForm.setState("0");
                listForm.setHasfile("0");
                if ("1".equals(listForm.getSfdz())) {
                    listForm.setDisabled("1");
                } else {
                    listForm.setDisabled("0");
                }
            }
        }
        if(sjclList.size()>0)
		{
				YwComSjcllistForm sjclForm01=sjclList.get(0);
				form.setTitleRow101(sjclForm01.getTitle());
				form.setTitleRow102(sjclForm01.getTitle());
				form.setFyjsRow101(sjclForm01.getFyjs());
				form.setFyjsRow102(sjclForm01.getFyjs());
				form.setFsRow101(sjclForm01.getYjs()+sjclForm01.getFyjs());
				form.setFsRow102(sjclForm01.getYjs()+sjclForm01.getFyjs());
				if("0".equals(sjclForm01.getSfdz()))
				{
					form.setSfdzRow101("√");
					form.setSfdzRow102("√");
				}else{
					form.setSfdzRow101("√");
					form.setSfdzRow102("√");
				}
				
				YwComSjcllistForm sjclForm02=sjclList.get(1);
				form.setTitleRow201(sjclForm02.getTitle());
				form.setTitleRow202(sjclForm02.getTitle());
				form.setFyjsRow201(sjclForm02.getFyjs());
				form.setFyjsRow202(sjclForm02.getFyjs());
				form.setFsRow201(sjclForm02.getYjs()+sjclForm02.getFyjs());
				form.setFsRow202(sjclForm02.getYjs()+sjclForm02.getFyjs());
				if("0".equals(sjclForm02.getSfdz()))
				{
					form.setSfdzRow201("√");
					form.setSfdzRow202("√");
				}else{
					form.setSfdzRow201("√");
					form.setSfdzRow202("√");
				}
				
				
				YwComSjcllistForm sjclForm03=sjclList.get(2);
				form.setTitleRow301(sjclForm03.getTitle());
				form.setTitleRow302(sjclForm03.getTitle());
				form.setFyjsRow301(sjclForm03.getFyjs());
				form.setFyjsRow302(sjclForm03.getFyjs());
				form.setFsRow301(sjclForm03.getYjs()+sjclForm03.getFyjs());
				form.setFsRow302(sjclForm03.getYjs()+sjclForm03.getFyjs());
				if("0".equals(sjclForm03.getSfdz()))
				{
					form.setSfdzRow301("√");
					form.setSfdzRow302("√");
				}else{
					form.setSfdzRow301("√");
					form.setSfdzRow302("√");
				}
				
				YwComSjcllistForm sjclForm04=sjclList.get(3);
				form.setTitleRow401(sjclForm04.getTitle());
				form.setTitleRow402(sjclForm04.getTitle());
				form.setFyjsRow401(sjclForm04.getFyjs());
				form.setFyjsRow402(sjclForm04.getFyjs());
				form.setFsRow401(sjclForm04.getYjs()+sjclForm04.getFyjs());
				form.setFsRow402(sjclForm04.getYjs()+sjclForm04.getFyjs());
				if("0".equals(sjclForm04.getSfdz()))
				{
					form.setSfdzRow401("√");
					form.setSfdzRow402("√");
				}else{
					form.setSfdzRow401("√");
					form.setSfdzRow402("√");
				}
				
				
				YwComSjcllistForm sjclForm05=sjclList.get(4);
				form.setTitleRow501(sjclForm05.getTitle());
				form.setTitleRow502(sjclForm05.getTitle());
				form.setFyjsRow501(sjclForm05.getFyjs());
				form.setFyjsRow502(sjclForm05.getFyjs());
				form.setFsRow501(sjclForm05.getYjs()+sjclForm05.getFyjs());
				form.setFsRow502(sjclForm05.getYjs()+sjclForm05.getFyjs());
				if("0".equals(sjclForm05.getSfdz()))
				{
					form.setSfdzRow501("√");
					form.setSfdzRow502("√");
				}else{
					form.setSfdzRow501("√");
					form.setSfdzRow502("√");
				}
				
				
				YwComSjcllistForm sjclForm06=sjclList.get(5);
				form.setTitleRow601(sjclForm06.getTitle());
				form.setTitleRow602(sjclForm06.getTitle());
				form.setFyjsRow601(sjclForm06.getFyjs());
				form.setFyjsRow602(sjclForm06.getFyjs());
				form.setFsRow601(sjclForm06.getYjs()+sjclForm06.getFyjs());
				form.setFsRow602(sjclForm06.getYjs()+sjclForm06.getFyjs());
				if("0".equals(sjclForm06.getSfdz()))
				{
					form.setSfdzRow601("√");
					form.setSfdzRow602("√");
				}else{
					form.setSfdzRow601("√");
					form.setSfdzRow602("√");
				}
				
				YwComSjcllistForm sjclForm07=sjclList.get(6);
				form.setTitleRow701(sjclForm07.getTitle());
				form.setTitleRow702(sjclForm07.getTitle());
				form.setFyjsRow701(sjclForm07.getFyjs());
				form.setFyjsRow702(sjclForm07.getFyjs());
				form.setFsRow701(sjclForm07.getYjs()+sjclForm07.getFyjs());
				form.setFsRow702(sjclForm07.getYjs()+sjclForm07.getFyjs());
				if("0".equals(sjclForm07.getSfdz()))
				{
					form.setSfdzRow701("√");
					form.setSfdzRow702("√");
				}else{
					form.setSfdzRow701("√");
					form.setSfdzRow702("√");
				}
				
				
				YwComSjcllistForm sjclForm08=sjclList.get(7);
				form.setTitleRow801(sjclForm08.getTitle());
				form.setTitleRow802(sjclForm08.getTitle());
				form.setFyjsRow801(sjclForm08.getFyjs());
				form.setFyjsRow802(sjclForm08.getFyjs());
				form.setFsRow801(sjclForm08.getYjs()+sjclForm08.getFyjs());
				form.setFsRow802(sjclForm08.getYjs()+sjclForm08.getFyjs());
				if("0".equals(sjclForm08.getSfdz()))
				{
					form.setSfdzRow801("√");
					form.setSfdzRow802("√");
				}else{
					form.setSfdzRow801("√");
					form.setSfdzRow802("√");
				}
		}
        if(form.getHbbm()==null)
       	form.setHbbm("市城管委景观处");
        
       
        form.setSqdw(qyxxform.getSqdw());
        form.setGdName2(form.getGdName());
        form.setGdName3(form.getGdName());
        form.setContent(form.getGdName());
        form.setAddress2(form.getAddress());
        form.setLxr(form.getYwlxr());
        form.setYwlxrtel2(form.getYwlxrtel());
        form.setSqrq2(form.getSqrq());
        
        if("1".equals(form.getGgxz())){
        	form.setPrintGgxz("经营性广告 ");
        }else 
        if("2".equals(form.getGgxz())){
        	form.setPrintGgxz("自设性广告 ");
        }else 
        if("3".equals(form.getGgxz())){
        	form.setPrintGgxz("其它 ");
        }
        
        String ggxs="";
       
       if(form.getGgxs()!=null){
    	   String[] ggxss= form.getGgxs().split(",");
	       for(int i=0;i<ggxss.length;i++){
	    	   
	           if("1".equals(ggxss[i].trim())){
	           	ggxs+="展示牌,";
	           }
	           if("2".equals(ggxss[i].trim())){
	           	ggxs+="灯箱 ,";
	           }
	           if("3".equals(ggxss[i].trim())){
	           	ggxs+="霓虹灯,";
	           }
	           if("4".equals(ggxss[i].trim())){
	           	ggxs+="电子显示牌（屏）,";
	           }
	           if("5".equals(ggxss[i].trim())){
	           	ggxs+="实体字,";
	           }
	           if("6".equals(ggxss[i].trim())){
	           	ggxs+="充气物,";
	           }
	           if("7".equals(ggxss[i].trim())){
	           	ggxs+="模型,";
	           }
	           if("8".equals(ggxss[i].trim())){
	           	ggxs+="其它";
	           }
	       }
       }
        
        
        form.setPrintGgxs(ggxs);
        
        
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日");
        form.setCurDate2(simpleDateFormat.format(form.getSqrq()));
        form.setCurDate3(simpleDateFormat.format(form.getSqrq()));
        simpleDateFormat=new SimpleDateFormat("yyyy");
        form.setSerialNo2(form.getSerialNo());
        form.setPassword2(form.getPassword());
        form.setNo(simpleDateFormat.format(new Date())+""+ form.getAcceptNo().substring(form.getAcceptNo().length()-4));
        
        return form;
        //return YwHwShSqbConvertor.convertVoToForm(entity);
	}
	
	//通过案件号获得实体类
    public List<YwHwShSqb> getByAcceptNo(String acceptNo) {
    	String sql = "from YwHwShSqb t where t.acceptNo='" + acceptNo + "'";
        List<YwHwShSqb> entityList = ywHwShSqbDao.find(sql);
        return entityList;
    }
	
    public void changeYslState(Long id,Long state,String yslThyy) {
    	StringBuffer sql = new StringBuffer();
    	sql.append("update YwHwShSqb t set t.ysl="+state);
    	if(StringUtils.isNotBlank(yslThyy)){
    		sql.append(" ,t.yslThyy='"+yslThyy+"'");
    	}
    	sql.append(" where t.id="+id);
    	ywHwShSqbDao.getSession().createQuery(sql.toString()).executeUpdate();
    	System.out.println("户外广告收件");
    }
    
    //是否延期办理
    public void changeIsycbl(Long id, String state) {
//    	YwHwShSqbForm form = this.get(id);
//    	form.setIsycbl(state);
//    	this.save(form);
    	
    	YwHwShSqb entity = ywHwShSqbDao.get(id);
    	entity.setIsycbl(state);
    	ywHwShSqbDao.save(entity);
	}
    
    /**
     * 设置该案件为咨询服务案件
     * @param id
     */
    public void changeIsZxfwaj(Long id) {
//    	YwHwShSqbForm form = this.get(id);
//    	form.setYsl(state);
//    	this.save(form);
    	YwHwShSqb entity = ywHwShSqbDao.get(id);
    	entity.setIsZxfwaj("Y");
    	ywHwShSqbDao.save(entity);
    }
	
    //保存结案时间
	public void endSave(Long id) {
//		YwHwShSqbForm form = this.get(id);
//		form.setJarq(new Date());
//		this.save(form);
		
		YwHwShSqb entity = ywHwShSqbDao.get(id);
    	entity.setJarq(new Date());
    	ywHwShSqbDao.save(entity);
	}
    
	public void delete(Long... ids) {
		ywHwShSqbDao.delete(ids);
	}

	/**
	 * �����������޸ĵ�Form�����б�
	 */
	public void save(YwHwShSqbForm... forms) {
		if(forms != null)
			for(YwHwShSqbForm form : forms)
				this.save(form);
	}
	
	/**
	 * �����������޸ĵ�Form����
	 */
	public void save(YwHwShSqbForm form){
        if (form == null) return;

		YwHwShSqb entity = null;
        if(form != null && form.getId() != null){
            entity = ywHwShSqbDao.get(form.getId());
            if (StringUtils.isBlank(entity.getAcceptNo()) || form.getAcceptNo().indexOf("XXXX") > 0) form.setAcceptNo(null);
        }else{
            entity = new YwHwShSqb();
            form.setAcceptNo(null);
        }
        
        //保存企业信息
        YwComQyxxForm qyxxForm = new YwComQyxxForm();
        if(form.getQyid() == null){
        	if(StringUtils.isNotBlank(form.getSqdw())){
            	YwComQyxxForm ywComQyxxForm = ywComQyxxService.getFormBySqdw(form.getSqdw(), "hwggshsqb");
            	if(ywComQyxxForm != null){
            		qyxxForm = ywComQyxxForm;
            	}
            }
        }
        YwHwShSqbConvertor.convertFormToQyxxForm(form, qyxxForm);
        qyxxForm.setTemplateCode("hwggshsqb");
        ywComQyxxService.update(qyxxForm);
        form.setQyid(qyxxForm.getId());
        
        boolean isNewForm = false;
        if (StringUtils.isBlank(form.getAcceptNo())) {
        	//生成政务中心统一查询编号 开始
			Map<String,String> serialNoAndPasswordMap = null;
        	if("Y".equals(form.getSendNotice())){//网办，1
        		serialNoAndPasswordMap = ywMtbjlshInfoService.getSerialNoAndPassword("1");
        	}else{//大厅，0
        		serialNoAndPasswordMap = ywMtbjlshInfoService.getSerialNoAndPassword("0");
        	}
        	String serialNo = serialNoAndPasswordMap.get("serialNo");
        	String password = serialNoAndPasswordMap.get("password");
			form.setSerialNo(serialNo);
			form.setPassword(password);
			form.setAcceptNo(serialNo);
    		//生成政务中心统一查询编号 结束
            isNewForm = true;
        }
        String acceptNo="";
        if(entity!=null && StringUtils.isNotBlank(entity.getAcceptNo())){//有时会出现acceptno2和acceptno不一致
        	 acceptNo=entity.getAcceptNo();
        	 form.setAcceptNo(acceptNo);
        }
        YwHwShSqbConvertor.convertFormToVo(form, entity);
       
        ywHwShSqbDao.save(entity);
        form.setId(entity.getId());
        
        if (isNewForm) {
            sjclService.updateByNewForm(form.getId());
        }
	}

	/**
	 * ���Form����Ĳ�ѯ��������ҳ��ѯ
	 */
	@Transactional(readOnly = true)
	public Page<YwHwShSqbForm> search(Page<YwHwShSqbForm> page, YwHwShSqbForm form) {
		// �����µķ�ҳ����
		Page pg = PageUtils.newPageInstance(page);
		
		// ��ѯ��估����
		StringBuffer hql = new StringBuffer("from YwHwShSqb ps where 1=1");
		List values = new ArrayList();
		
		// ��ѯ����
		if(form != null){
			
		}
		
		//����
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		// ִ�з�ҳ��ѯ����
		ywHwShSqbDao.findPage(pg, hql.toString(), values);
		
		// ת��ΪForm�����б?��ֵ��ԭ��ҳ������
		List<YwHwShSqbForm> list = YwHwShSqbConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * ��ݹ��������б�����ҳ��ѯ
	 */
	@Transactional(readOnly = true)
	public Page<YwHwShSqbForm> search(Page<YwHwShSqbForm> page, List<PropertyFilter> filters) {
		// �����µķ�ҳ����
		Page<YwHwShSqb> pg = PageUtils.newPageInstance(page);
		
		// ������������ҳ���Ҷ���
		ywHwShSqbDao.findPage(pg, filters);
		
		// ת��ΪForm�����б?��ֵ��ԭ��ҳ������
		List<YwHwShSqbForm> list = YwHwShSqbConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * 通过ID号,改变窗口是否可打印受理通知书状态(1为是；0和空为否)
	 * @param acceptNo 受理号
	 * @return Page<YwRqSsgdSqb>
	 */
	public void changeSendAcceptNotice(Long id){
		YwHwShSqb entity = ywHwShSqbDao.get(id);
		entity.setSendAcceptNotice("1");
		ywHwShSqbDao.save(entity);
	}
	
	/**
	 * 通过ID号,改变窗口是否可打印延期办理结果通知书状态(1为是；0和空为否)
	 * @param id 
	 * @return 
	 */
	public void changeSendYqNotice(Long id){
		YwHwShSqb entity = ywHwShSqbDao.get(id);
		entity.setSendYqNotice("1");
		ywHwShSqbDao.save(entity);
	}
	
	/**
	 * 根据ID获得对象实体
	 * @param id
	 * @return
	 */  
	public YwHwShSqb getEntity(Long id){
		return ywHwShSqbDao.get(id);
	}
	
	/**
     * 保存对象实体
     * @param ywHwShSqb
     */     
    public void saveEntity(YwHwShSqb ywHwShSqb){
    	ywHwShSqbDao.save(ywHwShSqb);
    }
    
    public String wfBusYslTuih(YwHwShSqbForm form,LoginUserForm loginUser) throws Exception {
    	String  loginUserName = loginUser.getUser().getUserName();
    	WfBusInstanceTaskForm taskForm = new WfBusInstanceTaskForm();
		taskForm.setHandleComments("资料不全，不予受理。");
		//把审批状态数据保存到ywWebserviceInfo ，交换给方欣
		ywWebserviceInfoService.saveYwWebserviceInfo(form.getSqrq(), form.getAcceptNo(), form.getCbyw(), form.getYwlxr(), form.getSjr(), form.getSqdw(), 
				form.getBusinessCode(), form.getApprovalNo(), loginUserName, loginUser.getOrg().getOrgName(), "ysl", new Date(), false, "预受理", taskForm);
//		System.out.println("form.getYslThyy() = "+java.net.URLDecoder.decode(form.getYslThyy(),"UTF-8"));
		this.changeYslState(form.getId(), 0L,taskForm.getHandleComments());
		
		//（网办）交换到市民网页
		Map map=exchangeStateService.buildMap(loginUser, null, form.getId().toString(), form.getTemplateCode(), false);
		String durationExpression = wfServiceEX.getDurationExpressionByTemplateCode(form.getTemplateCode());
		ywWebserviceForWbService.setValueAndSaveYwWebserviceForWb(loginUserName,form.getTemplateCode(),
				form.getId().toString(),durationExpression,"001",taskForm.getHandleComments(), map);
    	return null;
    }


	@Override
	public void updateAcceptNo(YwHwShSqb form) {
		String hql = "update YwHwShSqb gd set gd.acceptNo='"+form.getAcceptNo()+"',gd.serialNo='"+form.getAcceptNo()+"' where gd.id=:id";
		Query query =ywHwShSqbDao.getSession().createQuery(hql);
		query.setLong("id", form.getId());
		query.executeUpdate();
		
	}
}
